package com.archives.blog.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.archives.blog.common.config.ApplicationConfig;
import com.archives.blog.common.domain.entity.ArticleChannel;
import com.archives.blog.common.domain.pojo.result.ImageResult;
import com.archives.blog.common.utils.FileUploadUtil;
import com.archives.blog.manager.ArticleChannelManager;

@Controller
public class CommonController extends BaseSupportController{
	
	@Autowired
	private ArticleChannelManager articleChannelManager;
	
	@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
	@ResponseBody
	public ImageResult fileUpload(MultipartFile file){
		ImageResult result = new ImageResult();
		try {
			FileUploadUtil.write(file.getInputStream(), ApplicationConfig.uploadConfig.getImg() + "/" + file.getOriginalFilename());
			result.setResultObject(file.getOriginalFilename());
			result.setImageUrl(ApplicationConfig.uploadConfig.getImgUrl() + "/" + file.getOriginalFilename());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping(value = "/channels", method = RequestMethod.POST)
	@ResponseBody
	public List<ArticleChannel> queryChannels(){
		List<ArticleChannel> channels = new ArrayList<ArticleChannel>();
		try {
			channels = articleChannelManager.getAll();
		} catch (Exception e) {
		}
		return channels;
	}
}
